package com.gym.controller;

import com.google.gson.Gson;
import com.gym.dto.BaseResult;
import com.gym.pojo.UserInfo;
import com.gym.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class LoginController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    RedisTemplate redisTemplate;

    //登录页面跳转
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    //跳转到主页
    @RequestMapping("/toIndex")
    public String toIndex() {
        return "index";
    }

//    //登录
    @RequestMapping("/login")
    @ResponseBody
    public BaseResult login(String username, String password, HttpSession session) {
        //todo service业务调用校验用户名密码
        ValueOperations operations = redisTemplate.opsForValue();
        Gson gson = new Gson();
        BaseResult baseResult = new BaseResult();
        UserInfo userInfo = userInfoService.queryByUsernameAndPassword(username, password);
        //将字符串转换成JSON
        String userJson = gson.toJson(userInfo);
//        String userJson = JSON.toJSONString(user);
        //获取登录时的错误次数
        String errorsNum = (String) operations.get("userInfo");
        //为空赋值
        if (errorsNum == null) {
            errorsNum = String.valueOf(Long.MAX_VALUE - 4);
        }
        Long number = Long.valueOf(errorsNum) - Long.MAX_VALUE;
        //用户名和密码为空或没达到错误次数就进行错误次数设定
        if (userInfo == null || number == 0) {
            //错误次数为空就设定次数
            if (operations.get("userInfo") == null) {
                //设置比最大数小 4 的数 等于第六次就会被锁起
                operations.set("userInfo", Long.MAX_VALUE - 4 + "", 30, TimeUnit.SECONDS);
                baseResult.setFlag(false);
                return baseResult;
            } else {
                try {
                    operations.increment("userInfo");
                }catch (Exception e) {
                    e.printStackTrace();
                    baseResult.setFlag(false);
                    baseResult.setData("当前页面已被锁定，请在30秒后再试！");
                }finally {
                    baseResult.setFlag(false);
                    return baseResult;
                }
            }
        } else {
            //没有达到最大错误次数 登录成功后就重置次数和时间
            Integer userCount = userInfoService.userCount();
            session.setAttribute("userCount",userCount);
            session.setAttribute("username",username);
            operations.set("userName",userJson);
            operations.set("userInfo",Long.MAX_VALUE - 5 + "",30,TimeUnit.SECONDS);
            baseResult.setFlag(true);
            baseResult.setData(userInfo);
            return baseResult;
        }
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(UserInfo userInfo) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = userInfoService.save(userInfo);
        if (save) {
            map.put("save", save);
            map.put("data", "注册成功");
        } else {
            map.put("save", save);
            map.put("data", "注册失败");
        }
        return map;
    }
}
